package com.litorg.demo.db.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.litorg.demo.db.config.OpenTSDBService;
import com.litorg.demo.db.mapper.ArticleDao;
import com.litorg.demo.db.mapper.TestMapper;
import com.litorg.demo.db.mapper.UserMapper;
import com.litorg.demo.db.utils.HttpResponse;
import com.litorg.demo.db.utils.RedisUtil;
import com.litorg.demo.db.vo.Article;
import com.litorg.demo.db.vo.Reader;
import com.litorg.demo.db.vo.TestVO;
import com.litorg.demo.db.vo.User;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.opentsdb.client.bean.request.SubQuery;
import org.opentsdb.client.bean.response.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Despriction:
 * @Author: wangcheng
 * @Date: 2022/3/21 19:55
 */
@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TestMapper testMapper;

    // @Autowired
    // private RedisUtil redisUtil;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ArticleDao articleDao;

    // @Autowired
    // private OpenTSDBService openTSDBService;

    @GetMapping("test")
    public String test(@RequestParam("message") String msg) {
        return "hello:" + msg;
    }

    @GetMapping("getUser")
    public String getUser(@RequestParam("message") String msg) {
        User user = userMapper.selectById(1);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "Jone");
        List<User> users = userMapper.selectList(queryWrapper);

        Page<User> page = new Page<>(2, 2);
        Page<User> userPage = userMapper.selectPage(page, null);
        return "hello:" + msg;
    }

    /*@GetMapping("getRedisInfo")
    public String getRedisInfo(@RequestParam("key") String key) {
//        if (Objects.isNull()) {
//            boolean test133 = redisUtil.set(key, "test133", 30);
//        }
        Object obj = redisUtil.get(key);
        return "hello:" + "";
    }*/

    @GetMapping("getaa")
    public List<TestVO> getaa(@RequestParam("table") String table, @RequestParam("equipmentId") String equipmentId) {
        List<TestVO> testVOS = testMapper.selectData(table);
        if (!CollectionUtils.isEmpty(testVOS)) {
            testVOS.forEach(item -> item.setEquipmentId(equipmentId));
            System.out.println(testVOS.size());
        }

        /*try {
            ObjectMapper objectMapper = new ObjectMapper();
            System.out.println(objectMapper.writeValueAsString(testVOS));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }*/
        return testVOS;
    }

    @GetMapping("getaabb")
    public List<TestVO> getaabb(HttpResponse response) {
        List<TestVO> testVOS = new ArrayList<>();
        response.JSON(401, "asdasdasdasd");
        return testVOS;
    }

    @GetMapping("mongo-add")
    public String mongoAdd(@RequestParam("message") String msg) {
        Article article = new Article();
        article.setAuthor("莫言");
        article.setContent("20世纪30年代初，山东高密地区土匪横行、民不聊生。东北乡破落地主家17岁的女儿九儿，被贪财的父亲许给有麻风病的酒坊主儿子单扁郎");
        article.setTitle("红高粱");
        Reader reader = new Reader();
        reader.setName("xiaomifeng1010");
        reader.setGender("男");
        List<Reader> readers = Arrays.asList(reader);
        article.setReader(readers);
//        Article save = articleDao.save(article);
        Article save = mongoTemplate.insert(article);
        return save.toString();
    }

    @PostMapping("mongo-update")
    public Long mongoUpdate(@RequestBody Article article) {
        //通过query根据id查询出对应对象，通过update对象进行修改
        Query query = new Query(Criteria.where("_id").is(article.getId()));
        Update update = new Update().set("msgContent", article.getContent());
//        Update update = new Update().set("username", article.getTitle());
        try {
            UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Article.class);
            return updateResult.getMatchedCount();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @GetMapping("mongo-del")
    public Long mongoDel(String id) {
        //通过query根据id查询出对应对象，通过update对象进行修改
        Query query = new Query(Criteria.where("_id").is(id));
        try {
            DeleteResult remove = mongoTemplate.remove(query, Article.class);
            return remove.getDeletedCount();
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        }
    }

    @GetMapping("mongo-find-one")
    public Article mongoFindOne(String id) {
        //通过query根据id查询出对应对象，通过update对象进行修改
        Query query = new Query(Criteria.where("_id").is(id));
        try {
            Article one = mongoTemplate.findOne(query, Article.class);
            return one;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*@GetMapping("testOpenTsdb")
    public List<QueryResult> testOpenTsdb(@RequestParam("metric") String metric) {
        Map<String, String> tags = new HashMap<>();
        tags.put("tag2", "tag2value");

        // boolean result = openTSDBService.put(metric, tags, 28.46);
        // System.out.println(result);

        List<QueryResult> results = openTSDBService.get(metric, tags, "1664160909", "1664245435", SubQuery.Aggregator.MAX);
        System.out.println(results.toString());
        return results;
    }*/

}
